package com.leviassaf.androidbasics;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity implements View.OnClickListener {

	Button btnResults;
	ToggleButton tglPassword;
	EditText edtCommands;
	TextView txtResults;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.text);
		
		//33
		setJavaRefs();
		
		//26-27,34
		tglPassword.setOnClickListener(this);
		//28,34
		btnResults.setOnClickListener(this);

		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this.getClass().getSimpleName()));
	}

	private void setJavaRefs() {
		// TODO Auto-generated method stub
		//25
		btnResults = (Button)findViewById(R.id.btnResults);
		tglPassword = (ToggleButton)findViewById(R.id.tglPassword);
		edtCommands = (EditText)findViewById(R.id.edtCommands);
		txtResults = (TextView)findViewById(R.id.txtResults);
	}

	//34
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()){
		case R.id.btnResults:
			String strCheck = edtCommands.getText().toString();
			
			//30
			txtResults.setText(strCheck);
			
			if (strCheck.toLowerCase().contentEquals("left")){
				txtResults.setGravity(Gravity.LEFT);
			}
			else if (strCheck.toLowerCase().contentEquals("center")){
				txtResults.setGravity(Gravity.CENTER);
			}
			else if (strCheck.toLowerCase().contentEquals("right")){
				txtResults.setGravity(Gravity.RIGHT);
			}
			else if (strCheck.toLowerCase().contentEquals("blue")){
				txtResults.setTextColor(Color.BLUE);
			}
			else if (strCheck.toUpperCase().contentEquals("WTF")){
				//31
				Random rand = new Random();
				txtResults.setText("WTF!!!");
				txtResults.setTextSize(rand.nextInt(75));
				txtResults.setTextColor(Color.rgb(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)));
				//32
				switch(rand.nextInt(3)){
				case 0:
					txtResults.setGravity(Gravity.LEFT);
					break;
				case 1:
					txtResults.setGravity(Gravity.CENTER);
					break;
				case 2:
					txtResults.setGravity(Gravity.RIGHT);
					break;
				}
			}
			else {
				txtResults.setText("Invalid entry");
				txtResults.setGravity(Gravity.CENTER);
				txtResults.setTextColor(Color.CYAN);
			}
			break;
		case R.id.tglPassword:
			if (tglPassword.isChecked()){
				edtCommands.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
			else{
				edtCommands.setInputType(InputType.TYPE_CLASS_TEXT);
			}
			break;
		}
	}
}
